package com.ipet.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class IpetCorsConfig {
    @Bean
    public CorsFilter corsFilter(){
        //1  添加CORS配置信息
        CorsConfiguration config = new CorsConfiguration();
        //2 设置跨域请求的对象
        config.addAllowedOrigin("http://manager.ipet.com");
        //3 设置是否发送Cookie信息
        config.setAllowCredentials(true);
        //4 设置允许的请求方式
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("PATCH");
        //5设置允许的头信息
        config.addAllowedHeader("*");
        //6 添加映射路径
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**",config);
        // 7 返回新的CorsFilter
        return new CorsFilter(configurationSource);
    }
}
